N = 1000000
isp = [1 for i in xrange(N * 10)]
p = []

isp[0], isp[1] = 0, 0

for i in xrange(2, N * 10):
    if isp[i]:
        if i < N:
            p.append(i)
        for j in xrange(i + i, N, i):
            isp[j] = 0

ans = 0

for n in p:
    sn = str(n)
    if len(sn) == 1:
        ans += 1
        continue
    good = 1
    for i in xrange(len(sn) - 1):
        sn = sn[-1] + sn[:-1]
        if not isp[int(sn)]:
            good = 0
            break
    if good:
        ans += 1
print ans
